package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.bo.GroupDO;
import com.partners.dao.dto.UserRoleOutputDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.GroupService;
import com.partners.utils.ApiResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 分组管理
 * @author liangyongle
 * @date 2018-12-29
 */
@RestController
@RequestMapping("group")
@Slf4j
public class GroupController {


    @Resource
    private GroupService groupService;

    /**
     * 获取用户分组
     * @param paging Paging
     * return
     * */
    @PostMapping("group_list")
    public ApiResult getGroupList(@BeanValid @RequestBody Paging paging,@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Map<String,Object> list = groupService.getGroupList(paging,id);
        return ApiResultUtil.ok(list);
    }

    /**
     * 获取所有分组和其它用户
     * @param uid  Integer
     * @return list
     * */
    @PostMapping("group_user")
    public ApiResult getUserGroup(@SimpleParam("uid") Integer uid,@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        if(uid == null){
            throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }
        Map<String,Object> list = groupService.getGroupUserList(uid,id);
        return ApiResultUtil.ok(list);
    }



    /**
     * 获取分组下面的用户1
     * @param
     * */
    @PostMapping("get_group_user")
    public  ApiResult getUserByGroupId(@BeanValid @RequestBody Paging paging,@SimpleParam("id") Integer id ){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Map<String,Object> list =groupService.getUserByGroupId(paging,id);
        return ApiResultUtil.ok(list);
    }

   /**
     * 编辑分组
     * @param
     * */
    @PostMapping("edit_group")
    public ApiResult editGroup(@RequestBody GroupDO groupDO){
         if(groupDO.getPid() == null){
             throw BusinessException.of(ErrorMessageEnum.PID_NOT_FOND);
         }
         if(groupDO.getName() == null){
             throw BusinessException.of(ErrorMessageEnum.NAME_NOT_FOUND);
         }
         Integer result = groupService.editGroup(groupDO);
         return ApiResultUtil.ok(result);
    }

    /**
     * 获取分组详情
     * @param
     * */
    @PostMapping("group_detail")
    public ApiResult groupDetail(@SimpleParam("id") Integer id){
        GroupDO groupDO = groupService.getGroupeDetail(id);
        return ApiResultUtil.ok(groupDO);
    }

    /**
     * 删除分组
     * @param
     * */
    @PostMapping("group_delete")
    public ApiResult groupDelete(@SimpleParam("id") Integer id){
        groupService.deleteGroup(id);
        return ApiResultUtil.ok(SuccessMessageEnum.OPERATION_SUCCESS);
    }

    /**
     * 用户加入分组
     * @param groupId Integer
     *        userId Integer
     * */
    @PostMapping("join_group")
    public ApiResult joinGroup(@SimpleParam("group_id") Integer groupId,
                               @SimpleParam("user_id") Integer userId){
        if(groupId == null){
            throw BusinessException.of(ErrorMessageEnum.GROUPID_NOT_FOUND);
        }
        if(userId == null){
            throw BusinessException.of(ErrorMessageEnum.USERID_NOT_FOUND);
        }
        groupService.joinGroupUser(groupId,userId);
        return ApiResultUtil.ok(SuccessMessageEnum.EDIT_SUCCESS);
    }

    /**
     * 用户退出分组
     * @Param
     *
    * */
    @PostMapping("leave_group")
    public ApiResult leaveGroup(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        groupService.leaveGroupUser(id);
        return ApiResultUtil.ok(SuccessMessageEnum.EDIT_SUCCESS);
    }




}
